<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="学号">
              <a-input v-model="queryParam.xh" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm" placeholder=""/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="异动类型">
                <a-select v-model="queryParam.ydlx" allowClear>
                  <a-select-option v-for="i in TYPE0070" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select v-model="queryParam.xslbm" allowClear>
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="异动前院系">
                <a-select v-model="queryParam.ssyxm" showSearch :filterOption="filterOption" allowClear @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="异动前专业">
                <a-select v-model="queryParam.sszym" showSearch :filterOption="filterOption" allowClear>
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="开始日期">
                <a-date-picker v-model="queryParam.createTimeFrom" style="width: 100%" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="结束日期">
                <a-date-picker v-model="queryParam.createTimeTo" style="width: 100%" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="异动前年级">
                <a-select v-model="queryParam.sznj" allowClear>
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="审核状态">
                <a-select v-model="queryParam.shzt" allowClear>
                  <a-select-option v-for="i in TYPE0124" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生来源">
                <a-select allowClear v-model="queryParam.xslxm">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="24" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" @click="exportExcel">导出</a-button>
              <a-upload
                v-hasPermission="'xjxx:xstxImp'"
                name="file"
                :action="uploadUrl"
                :showUploadList="false"
                :headers="{
                  Authorization: $store.state.account.token
                }"
                :beforeUpload="beforeUpload"
                @change="handleChange">
                <a-button type="primary">退学导入</a-button>
              </a-upload>
              <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
          <a-col :span="24" style="color:red">
            温馨提示：导入功能只针对退学，导入字段：学号、异动原因。
            <!-- <a href=""></a> -->
            <a type="link" :href="tplHref">下载模板</a>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- <college-edit
      ref="collegeEdit"
      :visible.sync="visible"
      :editId.sync="editId"
      :selectMenu.sync="selectMenu"
      :confirmLoading.sync="confirmLoading"
      @cancel="visible = false"
      @create="handleCreate"
    /> -->
    <s-table
      v-if="initDone"
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :scroll="{x: 1500}"
      :pageSize="parameter.pageSize"
      :pageNum="parameter.pageNum"
      :alert="{show: true}">

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
      </span>
      <template slot="xh" slot-scope="a,b">
        <a @click="direct(b)">{{a}}</a>
      </template>
      <template slot="operation" slot-scope="a, b">

          <!--<a v-if="b.shztm !== '03'" @click="toEdit(b)">修改</a>-->
          <!--<a-divider v-if="b.shztm !== '03'" type="vertical" />-->
          <a @click="toInfo(b)">查看</a>
          <a-divider type="vertical" />
          <a v-if="b.shztm === '03'" @click="deleteTransfering(b)">删除</a>
          <a v-if="b.ydly === '2'" @click="exportCheckForm(b)">导出审批表</a>
      </template>
    </s-table>

  </a-card>
</template>

<script>
import { STable } from '~'
// import collegeEdit from './collegeEdit'
import { mapState } from 'vuex'
import searchMixin from '@/utils/searchRemain'

export default {
  mixins: [ searchMixin ],
  name: 'TableList',
  components: {
    STable
    // collegeEdit
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      visible: false,
      editId: null,
      // 查询参数
      queryParam: {},
      // 表头
      uploadUrl:`${process.env.SERVICE_URL}schoolRoll/xjxx/xstxImp`,
      tplHref:`${process.env.IMG_URL}tpl/xjyddrtx.xls`,
      columns: [
        {
          title: '序号',
          width: 80,
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '学号',
          dataIndex: 'xh',
          scopedSlots: { customRender: 'xh' }
        },
        {
          title: '异动前姓名',
          dataIndex: 'yxm'
        },
        {
          title: '异动前院系',
          dataIndex: 'yyxsmc'
        },
        {
          title: '异动前专业',
          dataIndex: 'yzymc'
        },
        {
          title: '异动前年级',
          width: 120,
          dataIndex: 'ynj'
        },
        {
          title: '异动类型',
          dataIndex: 'ydlbmc'
        },
        {
          title: '异动日期',
          dataIndex: 'ydrq'
        },
        {
          title: '异动前培养层次',
          dataIndex: 'ypyccmc'
        },
        {
          title: '审核状态',
          dataIndex: 'shztmc'
        },
        {
          title: '操作',
          fixed: 'right',
          width: 160,
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign({}, this.queryParam, parameter)
        if (this.queryParam.createTimeFrom) {
          params.createTimeFrom = this.queryParam.createTimeFrom.format('YYYY-MM-DD')
        }
        if (this.queryParam.createTimeTo) {
          params.createTimeTo = this.queryParam.createTimeTo.format('YYYY-MM-DD')
        }
        return this.$api.student.transferingList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRows: [],
      options: {
        alert: { show: false }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      confirmLoading: false,
      selectMenu: {},
      initDone: false
    }
  },
  computed: {
    ...mapState({
      TYPE0070: state => state.app['TYPE0070'], // 异动类型
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0124: state => state.app['TYPE0124'], // 审核状态
      TYPE0503: state => state.app['TYPE0503'],

    })
  },
  // beforeRouteEnter (to, from, next) {
  //   next(vm => {
  //     let pageParams = vm.$store.state.app.pageParams || {}
  //     if (from.path === '/student/stuStatus/search/stuView' && pageParams.path === vm.$route.path) {
  //       vm.queryParam = pageParams.params
  //       vm.parameter = pageParams.pagination
  //     }
  //     vm.initDone = true
  //   })
  // },
  // beforeRouteLeave (to, from, next) {
  //   if (to.path === '/student/stuStatus/search/stuView') {
  //     this.$store.dispatch('app/setPageParams', {
  //       path: this.$route.path,
  //       params: this.queryParam,
  //       pagination: this.parameter
  //     })
  //   }
  //   next()
  // },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0070')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0124')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')

  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    handleCreate () {
      const form = this.$refs.collegeEdit.form
      form.validateFields(async (err, values) => {
        if (err) {
          return
        }
        if (values.sxrq) {
          values.sxrq = values.sxrq.format('YYYY-MM-DD')
        }
        if (values.jlny) {
          values.jlny = values.jlny.format('YYYY-MM-DD')
        }
        this.confirmLoading = true
        let baseApi = this.editId ? 'collegeUpdate' : 'collegeAdd'
        let res = await this.$api.base[baseApi](Object.assign({id: this.editId}, values))
        this.$message.success(res.msg)
        this.$refs.table.refresh(true)
        this.confirmLoading = false
        form.resetFields()
        this.visible = false
      })
    },
    beforeUpload (file) {
      if (file.name.split('.')[1] !== 'xls') {
        this.$message.error('只支持.xls格式')
        return false
      }
      // console.log(file)
      // const isJPG = file.type === 'image/jpeg'
      // if (!isJPG) {
      //   returnthis.$message.error('You can only upload JPG file!')
      // }
      // const isLt2M = file.size / 1024 / 1024 < 2
      // if (!isLt2M) {
      //   this.$message.error('Image must smaller than 2MB!')
      // }
      // return isJPG && isLt2M
    },
    handleChange (info) {
      console.log(info)
      if (info.file.status === 'done') {
        this.$message.success('导入成功');
        this.$refs.table.refresh(true)
      } else if (info.file.status === 'error') {
        this.$message.error(info.file.response.msg || '导入失败');
      }
    },
    openAdd () {
      this.editId = null
      this.visible = true
      this.$refs.collegeEdit.form.resetFields()
    },
    openEdit (college) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.collegeEdit.form.setFieldsValue(college)
        this.editId = college.id
      })
    },
    resetForm () {
      this.queryParam = {}
      this.selectMenu.zy = []
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    direct (data) {
      this.$router.push(`/student/stuStatus/search/stuView?xh=${data.xh}`)
    },
    toEdit (item) {
      this.$router.push(`/student/change/save?id=${item.id}`)
    },
    deleteTransfering (item) {
      this.$confirm({
        title: '确定删除所选中的此学籍异动信息吗?',
        content: '当您点击确定按钮后，此学籍异动信息将会被彻底删除',
        centered: true,
        onOk: () => {
          this.$api.student.transferingDelete(item.id).then(() => {
            this.$message.success('删除成功')
            this.$refs.table.refresh(false)
          })
        },
        onCancel: () => {
        }
      })
    },
    toInfo(item) {
      this.$router.push(`/student/change/view?id=${item.id}`)
    },
    exportExcel() {
      let params = {...this.queryParam}
      delete params.pageNum
      this.$DateToString(params)
      this.$export('schoolRoll/xjxx/transferingExp', params)
    },
    exportCheckForm(b) {
      this.$export(`schoolRoll/xjxx/xjydPrint?id=${b.id}`)
      // window.location.href = `/api/schoolRoll/xjxx/xjjdPrint?id=${b.id}`
    }
  }
}
</script>
